﻿Imports System
Imports System.CodeDom
Imports System.Collections.Generic
Imports System.Text
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.Activities.Rules
    <Serializable> _
    Public Class RuleStatementAction
        Inherits RuleAction
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal codeDomExpression As CodeExpression)
            Me.codeDomStatement = New CodeExpressionStatement(codeDomExpression)
        End Sub

        Public Sub New(ByVal codeDomStatement As CodeStatement)
            Me.codeDomStatement = codeDomStatement
        End Sub

        Public Overrides Function Clone() As RuleAction
            Dim action As RuleStatementAction = DirectCast(MyBase.MemberwiseClone, RuleStatementAction)
            action.codeDomStatement = CodeDomStatementWalker.Clone(Me.codeDomStatement)
            Return action
        End Function

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim action As RuleStatementAction = [TryCast](obj,RuleStatementAction)
            Return ((Not action Is Nothing) AndAlso CodeDomStatementWalker.Match(Me.CodeDomStatement, action.CodeDomStatement))
        End Function

        Public Overrides Sub Execute(ByVal context As RuleExecution)
            If (Me.codeDomStatement Is Nothing) Then
                Throw New InvalidOperationException(Messages.NullStatement)
            End If
            CodeDomStatementWalker.Execute(context, Me.codeDomStatement)
        End Sub

        Public Overrides Function GetHashCode() As Integer
            Return MyBase.GetHashCode
        End Function

        Public Overrides Function GetSideEffects(ByVal validation As RuleValidation) As ICollection([Of] String)
            Dim analysis As New RuleAnalysis(validation, True)
            If (Not Me.codeDomStatement Is Nothing) Then
                CodeDomStatementWalker.AnalyzeUsage(analysis, Me.codeDomStatement)
            End If
            Return analysis.GetSymbols
        End Function

        Public Overrides Function ToString() As String
            If (Me.codeDomStatement Is Nothing) Then
                Return ""
            End If
            Dim stringBuilder As New StringBuilder
            CodeDomStatementWalker.Decompile(stringBuilder, Me.codeDomStatement)
            Return stringBuilder.ToString
        End Function

        Public Overrides Function Validate(ByVal validator As RuleValidation) As Boolean
            If (validator Is Nothing) Then
                Throw New ArgumentNullException("validator")
            End If
            If (Me.codeDomStatement Is Nothing) Then
                Dim error As New ValidationError(Messages.NullStatement, &H53D)
                [error].UserData.Item("ErrorObject") = Me
                validator.AddError([error])
                Return False
            End If
            Return CodeDomStatementWalker.Validate(validator, Me.codeDomStatement)
        End Function


        ' Properties
        Public Property CodeDomStatement As CodeStatement
            Get
                Return Me.codeDomStatement
            End Get
            Set(ByVal value As CodeStatement)
                Me.codeDomStatement = value
            End Set
        End Property


        ' Fields
        Private codeDomStatement As CodeStatement
    End Class
End Namespace

